package com.spdg.ring.task;

import com.spdg.ring.common.Config;
import com.spdg.ring.resp.PushResp;

import android.content.Context;
import cn.wolf.base.BaseTask;
import cn.wolf.http.BaseResp;
import cn.wolf.http.HttpCallBack;
import cn.wolf.http.HttpRequest;
import cn.wolf.notify.NotificationEntity;
import cn.wolf.tools.AppHelper;
import cn.wolf.tools.JsonUtil;
import cn.wolf.tools.Log;
import cn.wolf.tools.StringUtil;

/**
 * <p>消息推送Task      </p>
 * <p>Description: 
 *  异步请求推送消息
 * </p>
 * <p>@author: Eric.wsd                </p>
 * <p>Copyright: Copyright (c) 2012    </p>
 * <p>Company: FFCS Co., Ltd.          </p>
 * <p>Create Time: 2012-10-23             </p>
 * <p>Update Time:                     </p>
 * <p>Updater:                         </p>
 * <p>Update Comments:                 </p>
 */
public class PushTask extends BaseTask<Void, Void, Void> {

	public PushTask(HttpCallBack<PushResp> iCall, Context context) {
		super(iCall, context);
	}

	@Override
	protected BaseResp doInBackground(Void... params) {

		HttpRequest request = new HttpRequest(PushResp.class);
		PushResp resp = new PushResp();
		try {
			String serial = AppHelper.getSerialCode();
			if (StringUtil.isEmpty(serial)) {
				serial = AppHelper.getIMEI(mContext);
			}
			if (!StringUtil.isEmpty(serial)) {
				String url = Config.URL.URL_NOTIFICATION;
				// String url = Config.URL.URL_NOTIFICATION;
				request.addParameter("device", serial);
				request.addParameter("appid", AppHelper.getPackageName(mContext));
				resp = (PushResp) request.execute(url);
				if (resp.isSuccess()) {
					String result = resp.getHttpResult();
					if (!StringUtil.isEmpty(result)) {
						NotificationEntity entity = JsonUtil.toObject(result,
								NotificationEntity.class);
						resp.setmNotice(entity);
					}
				}
			}
		} catch (Exception ex) {
			Log.e("exception is :" + ex);
			resp.setStatus(BaseResp.ERROR);
		} finally {
			request.release();
		}

		return resp;
	}

}
